<include file="public:header" />
<include file="public:sidebar" />
<link rel="stylesheet" href="__STATIC__/summernote/summernote.css">
<script src="__STATIC__/summernote/summernote.min.js" type="text/javascript" charset="utf-8"></script>
<!-- <script type="text/javascript" charset="utf-8" src="__STATIC__/ueditor/ueditor.config.js?ver=1.2"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/ueditor/lang/zh-cn/zh-cn.js"></script> -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            活动管理
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">活动</li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="col-md-12">
            <div style="margin: 10px;">
                <button onclick="history.go(-1)" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger">
                    <i class="layui-icon">&#xe65c;</i>返回
                </button>
            </div>
            <!-- Horizontal Form -->
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">添加/修改活动</h3>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <form action="{:Url('article/insert')}" method="post" class="form-horizontal layui-form"
                    autocomplete="off">
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                <span class="text-danger">*</span>活动标题
                            </label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" placeholder="活动标题" name="title"
                                    lay-verify="required" value="<notempty name='info'>{$info.title}</notempty>">
                                <input type="hidden" name="id" value="<notempty name='info'>{$info.id}</notempty>">
                            </div>
                        </div>
                        <empty name="mc_id">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    <span class="text-danger">*</span>商户
                                </label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" placeholder="商户" name="mch"
                                        lay-verify="required" value="<notempty name='info'>{$info.mch}</notempty>">
                                    <input type="hidden" name="mcid"
                                        value="<notempty name='info'>{$info.mcid}</notempty>">
                                </div>
                            </div>
                            <else />
                            <input type="hidden" name="mcid" value="{$mc_id}">
                        </empty>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                <span class="text-danger">*</span>公众号
                            </label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" placeholder="公众号" name="extend"
                                    lay-verify="required" value="<notempty name='info'>{$info.extend}</notempty>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                发布人
                            </label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="author" placeholder="发布人"
                                    value="<notempty name='info'>{$info.author}</notempty>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                活动简介
                            </label>
                            <div class="col-sm-4">
                                <textarea name="description" placeholder="活动简介"
                                    class="form-control"><notempty name='info'>{$info.description}</notempty></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                <span class="text-danger">*</span>封面图
                            </label>
                            <div class="col-sm-4">
                                <button type="button" class="layui-btn" id="test1">
                                    <i class="layui-icon">&#xe67c;</i>上传图片
                                </button>
                                <div class="show-image"
                                    style="<empty name='info.image'>display:none;</empty> margin-top: 10px;">
                                    <input type="hidden" name="image" id="image"
                                        value="<notempty name='info'>{$info.image}</notempty>">
                                    <div style="position: relative;">
                                        <i class="fa fa-close" style="position: absolute;
                                            left:160px;top:-10px;cursor: pointer;"></i>
                                        <img src="<notempty name='info'>{$info.image}</notempty>" class="images"
                                            width="120">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                               我的二维码
                            </label>
                            <div class="col-sm-4">
                                <button type="button" class="layui-btn" id="test2">
                                    <i class="layui-icon">&#xe67c;</i>上传图片
                                </button>
                                <div class="show-image1" style="<empty name='info.wechat_image'>display:none;</empty> margin-top: 10px;">
                                    <input type="hidden" name="wechat_image" id="wechat_image"
                                           value="<notempty name='info'>{$info.wechat_image}</notempty>">
                                    <div style="position: relative;">
                                        <i class="fa fa-close" style="position: absolute;
                                            left:160px;top:-10px;cursor: pointer;"></i>
                                        <img src="<notempty name='info'>{$info.wechat_image}</notempty>" class="wechat_image" width="120">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                <span class="text-danger">*</span>主要内容
                            </label>
                            <div class="col-sm-8">
                                <!-- <textarea id="editor" name="content"
                                    style="height: 350px"><notempty name='info'>{$info.content|htmlspecialchars_decode}</notempty></textarea> -->
                                    <!-- <notempty name='info'>{$info.content|htmlspecialchars_decode}</notempty> -->
                                <textarea name='content' id="summernote_content"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                活动截止时间
                            </label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" readonly name="finish_time"
                                    value="<notempty name='info'>{$info.finish_time}</notempty>" lay-verify="required"
                                    <empty name='info'>id="date"</empty>>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                浏览量
                            </label>
                            <div class="col-sm-2">
                                <input type="number" class="form-control" name="hits1"
                                   value="<notempty name='info'>{$info.hits1}</notempty>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                                好看
                            </label>
                            <div class="col-sm-2">
                                <input type="number" class="form-control" name="love"
                                   value="<notempty name='info'>{$info.love}</notempty>"/>
                            </div>
                        </div>
                        <notempty name="$info">
                            <if condition="$mc_id neq 0 and $info.status eq 4">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        失败原因
                                    </label>
                                    <div class="col-sm-10">
                                        <p class="form-control-static">{$info.fail_info}</p>
                                    </div>
                                </div>
                            </if>
                            <if condition="$mc_id eq 0">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        发布
                                    </label>
                                    <div class="col-sm-10">
                                        <eq name="info.status" value="2">
                                            <input type="radio" name="status" checked value="2">是
                                            <input type="radio"  name="status" value="3">否
                                        </eq>
                                        <eq name="info.status" value="3">
                                            <input type="radio" name="status" value="2">是
                                            <input type="radio" name="status" checked value="3">否
                                        </eq>
                                        <eq name="info.status" value="1">
                                            <input type="radio" name="status" value="2">是
                                            <input type="radio" name="status" checked value="3">否
                                        </eq>
                                    </div>
                                </div>
                            </if>
                        </notempty>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">
                            </label>
                            <div class="col-sm-4">
                                <button type="submit" class="btn btn-info" lay-submit lay-filter="*">确定</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
    <!-- /.content -->
 </div>
<style>
    .layui-form-radio {
         margin: 0;
         padding-right: 0;
    }
</style>
<script>
    /* var ue = UE.getEditor('editor', {
        autoHeightEnabled: false,
        autoFloatEnabled: false,
        allowDivTransToP: false,
        wordCount: false,
        elementPathEnabled: false,
        maximumWords: 999999999
    }); */
    $('#summernote_content').summernote({
        minHeight: 250,
        lang: 'zh-CN',
        focus: false,
        toolbar: [
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['misc', ['fullscreen', 'codeview']]
        ],
        callbacks: {
            onImageUpload: function(files, editor) {
                console.log(files);
            }
        }
    });
    
    function escapeHTML(str) {
        var con = '';
        con = str.replace(/&lt;/g, '<');
        con = con.replace(/&amp;/g, '&');
        con = con.replace(/&gt;/g, '>');
        con = con.replace(/&#39;/g, "'");
        con = con.replace(/&quot;/g, '"');
        return con;
    };
    <notempty name='info'>
        var content = escapeHTML(`{$info.content|htmlspecialchars_decode}`);
        $('#summernote_content').summernote('code', content);
    </notempty>

    // 导入微信文章
    $('.note-misc').append('<button type="button" class="note-btn btn btn-default btn-sm" id="importwx">一键导入微信文章</button>');

    layui.use(['form', 'layer', 'upload', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            laydate = layui.laydate;
        laydate.render({
            elem: '#date',
            type: 'datetime'
        });
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1',
            field: 'image',
            url: '{:Url("uploadify/upload")}',
            before: function (obj) {
                layer.load();
            },
            done: function (res) {
                layer.closeAll('loading');
                if (res.status === 1) {
                    $('img.images').attr('src', res.path);
                    $('.show-image').show();
                }
            },
            error: function () {
                layer.closeAll('loading');
            }
        });
        var uploadInst = upload.render({
            elem: '#test2',
            field: 'image',
            url: '{:Url("uploadify/upload")}',
            before: function (obj) {
                layer.load();
            },
            done: function (res) {
                layer.closeAll('loading');
                if (res.status === 1) {
                    $('img.wechat_image').attr('src', res.path);
                    $('.show-image1').show();
                }
            },
            error: function () {
                layer.closeAll('loading');
            }
        });
        form.on('submit(*)', function (data) {
            data.field.image = $('img.images').attr('src');
            data.field.wechat_image = $('img.wechat_image').attr('src');
            if(!data.field.image){
                layer.msg('请上传先封面图');
                return false;
            }
            var index = layer.load();
            // console.log(data.field);
            // console.log($('#summernote_content').code());
            data.field['content'] = $('.note-editable').html();
            // return
            $.post(data.form.action, data.field, function (result) {
                layer.close(index);
                if (result.code) {
                    layer.msg(result.msg);
                    return false;
                }
                layer.alert(result.msg, {
                    icon: 6,
                    end: function () {
                        location.href = result.data.redirect;
                    }
                });
            });
            return false;
        });
        $(document).on('click', '.fa-close', function (e) {
            e.preventDefault();
            $(this).siblings('img').attr('src', '');
            $(this).parent('div').parent('div').hide();
        });
        
        // 导入文章事件
        $("body").on('click', '#importwx', function(e) {
            e.preventDefault();
            var url = prompt('请输入微信文章url')
            if(url) {
                layer.load(0, {shade: false})
                $.ajax({
                    url: "http://money.cnskycc.com:3000/users",
                    data: { url: url },
                    success: function(res) {
                        if(res.code == 0) {
                            $('#summernote_content').summernote('code', res.data);
                            $('.note-editing-area').find('img').each(function(ind, ele) {
                                var name = $(this).attr('data-src');
                                $(this).attr('src', name);
                            })
                        } else {
                            layer.msg('爬取失败');
                        }
                        layer.closeAll();
                    },
                    error: function(err) {
                        layer.msg('网络错误');
                        layer.closeAll();
                    }
                })
            }
        })
    });
</script>
<include file="public:footer" />